//
//  Bullet.m
//  DoodleGunnyChipMunk
//
//  Created by QuickSoluion on 5/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Bullet.h"
#import "BTBworld.h"
#import "GameConfig.h"

@implementation Bullet

+(id) BulletWithGame:(BTBworld*)_world
{
	return [[[self alloc] initWithGame:_world] autorelease];
}
+(id) BulletWithGame:(BTBworld*)_world shape:(cpShape*)shape;
{
	return [[[self alloc] initWithGame:_world shape:shape] autorelease];	
}
-(id) initWithGame:(BTBworld*)_world
{
	cpShape *shape = [_world._SMG addCircleAt:cpvzero mass:STATIC_MASS radius:7];
	return [self initWithGame:_world shape:shape];
}
-(id) initWithGame:(BTBworld*)_world shape:(cpShape*)shape
{
	[super initWithShape:shape file:@"rocket_2.png"];
	
	_worldBTB = _world;
	//create effect
	
	ParticleAnimation * temp =[[ParticleAnimation alloc] init];
	[temp initParticleEmitter_Galaxy];
	_emitter=[temp particleEmiiterAt_Galaxy:ccp(self.position.x+20,self.position.y+20) afterDelay:0.1];//900.20
	[self addChild:_emitter z:200000];
	[temp release];
	
	//Free the shape when we are released
	self.spaceManager = _world._SMG;
	self.autoFreeShape = YES;
	//Handle collisions
	shape->collision_type = kMissleColition;
	 
	
	return self;
}


-(void)Destroy:(SpaceManager *)_space:(cpBody *)_body
{
	tempSpace=_space;
	tempBody=_body;
	
	
	id f1 = [CCFadeTo actionWithDuration:.25 opacity:200];
	id f2 = [CCFadeIn actionWithDuration:.25];
	
	id d = [CCDelayTime actionWithDuration:0.1];
	id c = [CCCallFunc actionWithTarget:self selector:@selector(blowup)];
	
	[self runAction:[CCRepeatForever actionWithAction:[CCSequence actions:f1,f2,nil]]];
	[self runAction:[CCSequence actions:d,c,nil]];
	
	
}



-(void) blowup
{
	if(tempBody!=nil&&tempBody!=nil)
	{	
		[tempSpace removeAndFreeConstraintsOnBody:tempBody];
	}
	
	
	
	[self.spaceManager applyLinearExplosionAt:self.position radius:100 maxForce:1000000]; /// max force=4500
	
	CCParticleSun *explosion = [[[CCParticleSun alloc] initWithTotalParticles:100] autorelease];
	explosion.texture= [[CCTextureCache sharedTextureCache] addImage: @"stars.png"];
	
	explosion.position = self.position;
	explosion.duration = .5;
	explosion.speed = 40;
	explosion.autoRemoveOnFinish = YES;
	explosion.blendAdditive = YES;//NO
	[_worldBTB addChild:explosion];
	[self removeChild:_emitter cleanup:FALSE];
	[_worldBTB removeChild:self cleanup:YES];
}


@end
